package net.mcreator.makecustomitem.procedure;

import net.minecraftforge.fml.common.gameevent.TickEvent;
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;
import net.minecraftforge.fml.common.event.FMLPreInitializationEvent;
import net.minecraftforge.common.MinecraftForge;

import net.minecraft.world.World;
import net.minecraft.potion.PotionEffect;
import net.minecraft.init.MobEffects;
import net.minecraft.entity.EntityLivingBase;
import net.minecraft.entity.Entity;

import net.mcreator.makecustomitem.ElementsMakecustomitemMod;

import java.util.Map;

@ElementsMakecustomitemMod.ModElement.Tag
public class ProcedureSpeedAndRegenUse extends ElementsMakecustomitemMod.ModElement {
	public ProcedureSpeedAndRegenUse(ElementsMakecustomitemMod instance) {
		super(instance, 10);
	}

	public static void executeProcedure(Map<String, Object> dependencies) {
		if (dependencies.get("entity") == null) {
			System.err.println("Failed to load dependency entity for procedure SpeedAndRegenUse!");
			return;
		}
		Entity entity = (Entity) dependencies.get("entity");
		entity.getEntityData().setDouble("BuffGetTime", ((entity.getEntityData().getDouble("BuffGetTime")) + 1));
		if (((entity.getEntityData().getDouble("BuffGetTime")) >= 20)) {
			if ((((entity.getEntityData().getDouble("Speed")) > 0) && ((entity.getEntityData()
					.getDouble("Speed")) <= (((entity instanceof EntityLivingBase) ? ((EntityLivingBase) entity).getMaxHealth() : -1) / 10)))) {
				if (entity instanceof EntityLivingBase)
					((EntityLivingBase) entity).addPotionEffect(
							new PotionEffect(MobEffects.SPEED, (int) 20, (int) ((entity.getEntityData().getDouble("Speed")) - 1), (false), (false)));
			} else if (((entity.getEntityData()
					.getDouble("Speed")) > (((entity instanceof EntityLivingBase) ? ((EntityLivingBase) entity).getMaxHealth() : -1) / 10))) {
				if (entity instanceof EntityLivingBase)
					((EntityLivingBase) entity).addPotionEffect(new PotionEffect(MobEffects.SPEED, (int) 20,
							(int) ((((entity instanceof EntityLivingBase) ? ((EntityLivingBase) entity).getMaxHealth() : -1) / 10) - 1), (false),
							(false)));
			}
			if ((((entity.getEntityData().getDouble("Regen")) > 0) && ((entity.getEntityData()
					.getDouble("Regen")) <= (((entity instanceof EntityLivingBase) ? ((EntityLivingBase) entity).getMaxHealth() : -1) / 10)))) {
				if (entity instanceof EntityLivingBase)
					((EntityLivingBase) entity).addPotionEffect(new PotionEffect(MobEffects.REGENERATION, (int) 20,
							(int) ((entity.getEntityData().getDouble("Regen")) - 1), (false), (false)));
			} else if (((entity.getEntityData()
					.getDouble("Regen")) > (((entity instanceof EntityLivingBase) ? ((EntityLivingBase) entity).getMaxHealth() : -1) / 10))) {
				if (entity instanceof EntityLivingBase)
					((EntityLivingBase) entity).addPotionEffect(new PotionEffect(MobEffects.REGENERATION, (int) 20,
							(int) ((((entity instanceof EntityLivingBase) ? ((EntityLivingBase) entity).getMaxHealth() : -1) / 10) - 1), (false),
							(false)));
			}
			entity.getEntityData().setDouble("BuffGetTime", 0);
		}
	}

	@SubscribeEvent
	public void onPlayerTick(TickEvent.PlayerTickEvent event) {
		if (event.phase == TickEvent.Phase.END) {
			Entity entity = event.player;
			World world = entity.world;
			int i = (int) entity.posX;
			int j = (int) entity.posY;
			int k = (int) entity.posZ;
			java.util.HashMap<String, Object> dependencies = new java.util.HashMap<>();
			dependencies.put("x", i);
			dependencies.put("y", j);
			dependencies.put("z", k);
			dependencies.put("world", world);
			dependencies.put("entity", entity);
			dependencies.put("event", event);
			this.executeProcedure(dependencies);
		}
	}

	@Override
	public void preInit(FMLPreInitializationEvent event) {
		MinecraftForge.EVENT_BUS.register(this);
	}
}
